﻿<%@ WebHandler Language="C#" Class="wxMiniProgramLogin" %>
using Models;
using System;
using System.Web;
using Newtonsoft.Json;
using System.Linq;
using System.Web.Configuration;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Net;



public class wxMiniProgramLogin : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        var xxx=HttpContext.Current.Request.Form["paramJson"];
        JObject jo = (JObject)JsonConvert.DeserializeObject(xxx);
        string openid = context.Request.Headers["X-ACCESS-USER"];//Header中的token

        var goods_list = jo["listItem"];
        var address_id = int.Parse(jo["curAddressData"]["id"].ToString());
        var express_fee = Decimal.Parse(jo["express_fee"].ToString());

        var remark = jo["remark"].ToString();
        var OMsg = jo["OMsg"].ToString();
        var takemode=jo["takemode"].ToString();



        using (Entities db = new Entities())
        {
            var Uinfo = db.User.SingleOrDefault(p => p.UOpenID == openid);
            int parOrderID = 0;

            double Result = 0;
            for (int i = 0; i < goods_list.Count(); i++)
            {
                JObject goods = JObject.Parse(goods_list[i].ToString());
                S_Order OrderInfo = new S_Order();
                OrderInfo.UGUID = openid;   //ru
                OrderInfo.OCreatTime = DateTime.Now;

                // int num = int.Parse(goods["goods_num"].ToString());
                int  num = int.Parse(goods_list[i]["numb"].ToString());
                OrderInfo.ONumber = num;
                int GID = int.Parse(goods["id"].ToString());
                var mygood = db.S_Goods.SingleOrDefault(x => x.GID == GID);
                //mygood.GSoldCount += 1;
                var myprice=Decimal.Parse(goods["price"].ToString());
                OrderInfo.SPrice = myprice;
                OrderInfo.OLastPrice =myprice * num;
                OrderInfo.STruePrice =Convert.ToUInt16(mygood.GPriceYJ);

                OrderInfo.Oweight = mygood.GWeight;


                OrderInfo.SPName =mygood.GName;
                OrderInfo.SPImgUrl = methods.GetImgs(mygood.GImg)[0];

                OrderInfo.SPID = GID;
                //  OrderInfo.OStoreId = int.Parse(goods["storeid"].ToString());
                //if (mygood.FXPrice != null)
                //    OrderInfo.ParentFxprice = mygood.FXPrice;  //一级
                //if (mygood.FXPrice2 != null)
                //    OrderInfo.ParentParentFxprice = mygood.FXPrice2;  //上上上上级价格

                OrderInfo.OOrderNumber = methods.getOrderNumber(openid);//单号
                OrderInfo.OParentId = parOrderID;
                OrderInfo.OState = Uinfo.Ustate;
                OrderInfo.OendTime = DateTime.Now.AddDays(3);
                OrderInfo.OState = -1;
                OrderInfo.OPayState = 0;

                if (parOrderID == 0)
                {
                    var p = db.S_UADDRESS.FirstOrDefault(x => x.UAID == address_id);
                    OrderInfo.OProvance = p.UAProvance;
                    OrderInfo.OCity = p.UACity;
                    OrderInfo.ODistrict = p.UADistrict;
                    OrderInfo.OStreet = p.UAStreet;
                    OrderInfo.OAddress = p.UAddress + p.UDetailAddress;

                    OrderInfo.OpostPrice = express_fee;
                    OrderInfo.OPeople = p.UPeopleName;
                    OrderInfo.OTel = p.UAPhone;

                    OrderInfo.ORemarks = remark;
                    OrderInfo.OMsg = OMsg;

                    OrderInfo.OType = takemode;
                }
                else
                {
                    OrderInfo.OpostPrice=0;
                }



                db.S_Order.Add(OrderInfo);
                db.SaveChanges();

                if (parOrderID == 0)
                {
                    parOrderID = OrderInfo.OrderID;
                }

                Result += Convert.ToDouble(OrderInfo.SPrice * OrderInfo.ONumber + OrderInfo.OpostPrice);


            }
            var parOrder = db.S_Order.SingleOrDefault(p => p.OrderID == parOrderID);
            parOrder.OFinalPrice =Convert.ToDecimal(Result);

            if (Uinfo.UIndirectParentId != null)
            {
                parOrder.ParentOopenID=getParent(0, Uinfo.UIndirectParentId);   //直接推荐人
                var pp = db.User.SingleOrDefault(p => p.UOpenID ==  Uinfo.UIndirectParentId);
                if (pp != null && pp.UIndirectParentId != null) {
                    parOrder.ParentParentOopenID = getParent(0,pp.UIndirectParentId);        //间接接推荐人

                    var ppp = db.User.SingleOrDefault(p => p.UOpenID == pp.UIndirectParentId);
                    if (ppp != null && ppp.UIndirectParentId != null)
                    {
                        parOrder.ParentParentParentOopenID = getParent(0, ppp.UIndirectParentId);        //大间接接推荐人
                    }

                }
            }

            db.SaveChanges();





            var code = 1;
            var msg = "订单添加成功";
            var time = DateTime.Now;
            var data = new
            {
                id = parOrderID,
                type = "goods",
                status_name = "待付款",
            };
            context.Response.Write(JsonConvert.SerializeObject(new { code,msg,time,data}));
            HttpContext.Current.Response.End();

            //   sendSucceMsg(_rky.getOpenIDByguid(UparentId), parOrderID.ToString(), uguid);
            //  sendSucceMsgTome(_rky.getOpenIDByguid(uguid), parOrderID.ToString(), UparentId);
        }
    }


    public  string getParent(int MYstat, string Dguid)
    {
        using (Entities db = new Entities())
        {
            var pUser = db.User.SingleOrDefault(p => p.UOpenID == Dguid); //panduan 上级
            if (pUser == null)
                return "8888";
            else                           //否则
            {
                if (pUser.Ustate > MYstat)
                {
                    return pUser.UOpenID;
                }
                else
                {
                    return "8888";
                }
            }
        }

    }




    public bool IsReusable {
        get {
            return false;
        }
    }

}